//
//  XSStatusViewModel.swift
//  WeiDo
//
//  Created by XISHAN on 16/3/7.
//  Copyright © 2016年 XISHAN. All rights reserved.
//

import UIKit

class XSStatusViewModel: NSObject {

    
    var status:XSStatus?{
        
        didSet{
            
            print("status didSet")
        }
    }
    
    // 用户认证
    var verifiedImage: UIImage?
    // 会员图标
    var membershipImage: UIImage?
    
    // 转发数量
    var transmitCountStr: String?
    // 评论数量
    var commentsCountStr: String?
    // 点赞数量
    var attitudesCountStr: String?

    // 转发微博的内容.包含前面的昵称
    var retweetedText: String?
    // 格式化好的来源字符串
    var sourceText: String?
    
    // 格式化好的时间
    var createText:String?{
        get{
            return self.status?.createdDate?.dateDescription
        }
    }
    
    init(status:XSStatus){
        super.init()
        self.status = status
        
        
        let verifiedType = status.user?.verified ?? -1
        
        switch verifiedType {
        case 1:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2,3,5:// 企业认证
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220: // 达人
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }

        // 设置会员图标
        if let mbrank = status.user?.mbrank where mbrank > 0 {
            membershipImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        
        // 计算转发评论赞的三个按钮显示的字符串
        // 先计算转发的内容
        
        transmitCountStr = stringWithCount(status.reposts_count, defaultString: "转发")
        commentsCountStr = stringWithCount(status.comments_count, defaultString: "评论")
        attitudesCountStr = stringWithCount(status.attitudes_count, defaultString: "赞")
        
        
        // 判断如果有转发微昵称和转发微博内容的话,就去设置转发微博内容
        if let screenName = status.retweeted_status?.user?.screen_name, text = status.retweeted_status?.text {
            retweetedText = "@\(screenName):\(text)"
        }else{
            retweetedText = "转发微博已被删除!"
        }

        
        //处理微博来源text
        changeSourceText(status.source)
    }
    
    
    private func stringWithCount(count: Int, defaultString: String) -> String {
     
        
        if count < 10000 {
            if count == 0 {
                return defaultString
            }else{
                return "\(count)"
            }
        }else{
            let result = CGFloat(count / 1000) / 10
            let resultStr = "\(result)万"
            
            if resultStr.containsString(".0") {
                return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            return resultStr
        }
    }

    //处理微博来源字段
    private func changeSourceText(source:String?){
        
        // <a href="http://weibo.com" rel="nofollow">新浪微博</a>
        guard let text = source else{
            return
        }
        if let startText = text.rangeOfString("\">")?.endIndex{
            if let endText = text.rangeOfString("</")?.startIndex{
                sourceText = "来自\(text.substringWithRange(startText..<endText))"
            }
        }
        
    }
}
